package com.yysjyr.logisticssystem.workbench.mapper;

import com.yysjyr.logisticssystem.workbench.entity.Order;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;

import java.util.List;
import java.util.Map;

public interface OrderMapper {

    /**
     * @param order 订单对象
     * @return : 成功的条数
     * @author: 吃货大人
     * @date: 2023/10/26
     * @time : 16:08
     * @description: 新增订单根据订单对象
     */
    @Insert("""
            INSERT INTO tbl_order (id, content, type, create_time, create_by, edit_flag, edit_time, edit_by, address, state, attraction_time, courier_id) 
              VALUES (#{id}, #{content}, #{type}, #{createTime}, #{createBy}, #{editFlag}, #{editTime}, #{editBy}, #{address}, #{state}, #{attractionTime}, #{courierId}) 
            """)
    int insertOrder(Order order);

    /**
     * @param orders 订单集合
     * @return : 成功的条数
     * @author: 吃货大人
     * @date: 2023/10/26
     * @time : 16:18
     * @description: 根据集合插入订单数据
     */

    @Insert("""
            <script>INSERT INTO tbl_order (id, content, type, create_time, create_by, edit_flag, edit_time, edit_by, address, state, attraction_time, courier_id)
                VALUES
                <foreach collection="list" item="order" separator=",">
                    (#{order.id}, #{order.content}, #{order.type}, #{order.createTime}, #{order.createBy}, #{order.editFlag}, #{order.editTime}, #{order.editBy}, #{order.address}, #{order.state}, #{order.attractionTime}, #{order.courierId})
                </foreach></script>
            """)
    int insertOrderList(List<Order> orders);

    /**
     * @author: 吃货大人
     * @date: 2023/10/27
     * @time : 8:16
     * @param id 订单id
     * @return : 订单对象
     * @description: 根据 id查询订单
     */
    @Select("""
            select id, content, type, create_time, create_by, edit_flag, edit_time, edit_by, address, state, attraction_time, courier_id
            from tbl_order 
            where id = #{id}
            """)
    Order selectOrderById(String id);

    /**
     * @author: 吃货大人
     * @date: 2023/10/27
     * @time : 8:17
     * @param
     * @return : 订单对象集合
     * @description: 查询所有订单对象
     */
    @Select("""
            select id, content, type, create_time, create_by, edit_flag, edit_time, edit_by, address, state, attraction_time, courier_id
            from tbl_order 
            """)
    List<Order> selectAllOrder();

    /**
     * @author: 吃货大人
     * @date: 2023/10/27
     * @time : 8:18
     * @param id 订单id
     * @return : 订单map
     * @description: 根据  id查询订单详细
     */
    @Select("""
            select
            o.id, o.content, dv.value as type, o.create_time, u1.name as create_by, o.edit_flag, o.edit_time, u2.name as edit_by, a.id as address_id,u4.name as address_create_by,a.sender,a.sender_address,a.receiver,a.receiver_address, dv1.value as state, o.attraction_time, u3.name as courier_id
            from tbl_order o
            left join tbl_dic_value dv on o.type = dv.id
            left join tbl_dic_value dv1 on o.state = dv1.id
            left join tbl_user u1 on o.create_by = u1.id
            left join tbl_user u2 on o.edit_by = u2.id
            left join tbl_user u3 on o.courier_id = u3.id
            join tbl_address a on o.address = a.id
            left join tbl_user u4 on a.create_by = u4.id
            where id = #{id}
            """)
    Map<String, String> selectOrderDetailById(String id);

    /**
     * @author: 吃货大人
     * @date: 2023/10/27
     * @time : 8:19
     * @param
     * @return : 订单对象map
     * @description: 查询所有订单对象详细
     */
    @Select("""
            select
            o.id, o.content, dv.value as type, o.create_time, u1.name as create_by, o.edit_flag, o.edit_time, u2.name as edit_by, a.id as address_id,u4.name as address_create_by,a.sender,a.sender_address,a.receiver,a.receiver_address, dv1.value as state, o.attraction_time, u3.name as courier_id
            from tbl_order o
            left join tbl_dic_value dv on o.type = dv.id
            left join tbl_dic_value dv1 on o.state = dv1.id
            left join tbl_user u1 on o.create_by = u1.id
            left join tbl_user u2 on o.edit_by = u2.id
            left join tbl_user u3 on o.courier_id = u3.id
            join tbl_address a on o.address = a.id
            left join tbl_user u4 on a.create_by = u4.id
            """)
    List<Map<String,String>> selectAllOrderDetail();

}
